package com.contract.config;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.contract.common.enums.ContractStateEnum;
import com.contract.common.util.CommonUtil;
import com.contract.mapper.BizContractInfoMapper;
import com.contract.pojo.bo.BaseEntity;
import com.contract.pojo.bo.BizContractInfo;
import com.contract.service.BizContractInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 定时任务配置
 *
 * @author liw
 * @version 1.0
 * @date 2022/9/10 16:24
 */
@Configuration
@EnableScheduling
@Slf4j
public class ScheduledConfig {

    @Autowired
    private BizContractInfoMapper bizContractInfoMapper;

    @Autowired
    private BizContractInfoService bizContractInfoService;

    // 每天的第3小时整执行一次
    @Scheduled(cron = "0 0 3 * * ?")
    @Transactional(rollbackFor = Exception.class)
    public void execute() {
        log.info("--ScheduledConfig--start");

        // 合同  待生效  ->  生效
        LambdaQueryWrapper<BizContractInfo> qw = new LambdaQueryWrapper<>();
        qw.eq(BizContractInfo :: getState, ContractStateEnum.P.getKey());
        qw.le(BizContractInfo :: getStart, LocalDate.now());
        qw.select(BizContractInfo :: getId, BizContractInfo :: getUserId);
        List<BizContractInfo> list = bizContractInfoMapper.selectList(qw);
        if (!CommonUtil.isEmpty(list)) {
            List<Long> idList = list.stream().map(BaseEntity::getId).collect(Collectors.toList());
            LambdaUpdateWrapper<BizContractInfo> uw =new LambdaUpdateWrapper<>();
            uw.in(BizContractInfo :: getId, idList);
            uw.set(BizContractInfo :: getState, ContractStateEnum.E.getKey());
            bizContractInfoMapper.update(null, uw);
            // 更新人员
            for (BizContractInfo info : list) {
                bizContractInfoService.updateUserState(info.getUserId());
            }
        }

        // 在职的变成非在职
        qw.clear();
        qw.eq(BizContractInfo :: getState, ContractStateEnum.E.getKey());
        qw.lt(BizContractInfo :: getInvalidDate, LocalDate.now());
        qw.select(BizContractInfo :: getId, BizContractInfo :: getUserId);
        list = bizContractInfoMapper.selectList(qw);
        if (!CommonUtil.isEmpty(list)) {
            List<Long> idList = list.stream().map(BaseEntity::getId).collect(Collectors.toList());
            LambdaUpdateWrapper<BizContractInfo> uw =new LambdaUpdateWrapper<>();
            uw.in(BizContractInfo :: getId, idList);
            uw.set(BizContractInfo :: getState, ContractStateEnum.U.getKey());
            bizContractInfoMapper.update(null, uw);
            // 更新人员
            for (BizContractInfo info : list) {
                bizContractInfoService.updateUserState(info.getUserId());
            }
        }
        log.info("--ScheduledConfig--end");
    }

}